#!/usr/bin/env python
# -*- coding: latin1 -*-

import math
import sys

arq_latency = open("../logs/latency.log")
dados_latency = []

for line in arq_latency:
  x = line.split(" ")
  x[0] = float(x[0])
  x[1] = float(x[1][0:-1])
  if x[0] < 60.0 or x[0] > 540.0:
    continue
  dados_latency += [x]

soma = 0.0
media = 0.0
n = float(len(dados_latency))

for row in dados_latency:
  soma += row[1]

media = soma/n
soma_quad = 0.0

for row in dados_latency:
  soma_quad += (row[1]-media)*(row[1]-media)

desvio_padrao = math.sqrt((1.0/(n-1))*soma_quad)

dados_jitter = []

for i in range(0,len(dados_latency)):
  if i > 0:
    dados_jitter.append([dados_latency[i][0], (dados_latency[i][1]-dados_latency[i-1][1])])
  dados_latency[i].append(desvio_padrao)


arq_latency = open("../logs/latency2.log", "w")
arq_jitter = open("../logs/jitter.log", "w")

for line in dados_latency:
  arq_latency.write(str(line[0]) + " " + str(line[1]) + " " + str(line[2]) + "\n")

for line in dados_jitter:
  arq_jitter.write(str(line[0]) + " " + str(line[1]) + "\n")

arq_jitter.close()
arq_latency.close()

